﻿package mortal.game.view.daily
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import extend.language.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.tools.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.map3D.sceneInfo.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;

    public class DailyUtil extends Object
    {
        public static const dailyRewardIndex:int = 3;
        public static var curArr:Array;

        public function DailyUtil()
        {
            return;
        }// end function

        public static function isInclude(param1:int, param2:int) : Boolean
        {
            return (param1 & param2) == param2;
        }// end function

        public static function getDataByGroup(param1:int, param2:Boolean = false, param3:Boolean = false) : Array
        {
            var _loc_5:TDailyViewConfig = null;
            var _loc_13:Array = null;
            var _loc_14:int = 0;
            var _loc_15:int = 0;
            var _loc_4:Array = [];
            var _loc_6:* = new Dictionary();
            var _loc_7:* = DailyConfig.instance.allDailyData;
            var _loc_8:* = Cache.instance.role.entityInfo.level;
            var _loc_9:* = ClockManager.instance.nowDate.day;
            var _loc_10:* = ClockManager.instance.serverOpenDateNum;
            var _loc_11:* = Cache.instance.daily;
            _loc_6["copy" + EDailyPro._ESingleCopy] = [];
            _loc_6["copy" + EDailyPro._ETeamCopy] = [];
            _loc_6["copy" + EDailyPro._EChallengeCopy] = [];
            _loc_6["copy" + EDailyPro._EDefenseCopy] = [];
            _loc_6["copy" + EDailyPro._ETowerCopy] = [];
            var _loc_12:* = Cache.instance.role.entityInfo.level;
            for each (_loc_5 in _loc_7)
            {
                
                if (DailyUtil.isInclude(_loc_5.displayGroup, param1))
                {
                    if (param3)
                    {
                        if (_loc_8 < _loc_5.minLevel)
                        {
                            continue;
                        }
                    }
                    if (param2 && isComplete(_loc_5))
                    {
                        continue;
                    }
                    if (param1 == EDailyType._EDailyTypeVitality)
                    {
                        if (_loc_5.openWeekDay && _loc_5.openWeekDay.indexOf(_loc_9.toString()) == -1)
                        {
                            continue;
                        }
                        if (_loc_12 < _loc_5.minLevel)
                        {
                            continue;
                        }
                    }
                    if (_loc_5.type >= EDailyPro._ESingleCopy && _loc_5.type <= EDailyPro._ETowerCopy)
                    {
                        _loc_6["copy" + _loc_5.type].push(_loc_5);
                        continue;
                    }
                    _loc_4.push(_loc_5);
                }
            }
            for each (_loc_13 in _loc_6)
            {
                
                _loc_13.sortOn("minLevel", Array.NUMERIC | Array.DESCENDING);
                _loc_14 = _loc_13.length;
                _loc_15 = 0;
                while (_loc_15 < _loc_14)
                {
                    
                    _loc_5 = _loc_13[_loc_15] as TDailyViewConfig;
                    if (param1 == 1 && EDailyPro._EDefenseCopy == _loc_5.type)
                    {
                        if (_loc_14 >= 2)
                        {
                            _loc_5 = _loc_13[_loc_14 - 2];
                            if (_loc_8 >= _loc_5.minLevel)
                            {
                                _loc_4.push(_loc_5);
                            }
                            _loc_5 = _loc_13[(_loc_14 - 1)];
                            if (_loc_8 >= _loc_5.minLevel)
                            {
                                _loc_4.push(_loc_5);
                            }
                            break;
                        }
                    }
                    if (_loc_15 == (_loc_14 - 1) || _loc_8 >= _loc_5.minLevel)
                    {
                        _loc_4.push(_loc_5);
                        break;
                    }
                    _loc_15++;
                }
            }
            _loc_4.sort(sort);
            return _loc_4;
        }// end function

        private static function sort(param1:TDailyViewConfig, param2:TDailyViewConfig) : int
        {
            if (param1.priority <= param2.priority)
            {
                return -1;
            }
            return 1;
        }// end function

        public static function oper(param1:TDailyViewConfig, param2:Boolean = false) : void
        {
            var evtName:String;
            var evtValue:*;
            var arr:Array;
            var info:TaskInfo;
            var arr2:Array;
            var mapId:int;
            var mapX:int;
            var mapY:int;
            var sceneInfo:SceneInfo;
            var sPassTo:SPassTo;
            var point:SPoint;
            var cache:DailyCache;
            var value:* = param1;
            var isFly:* = param2;
            if (value.operType == EDailyOperType._EDailyOperTypeModule)
            {
                if (value.operValue && value.operValue.indexOf(":"))
                {
                    arr = value.operValue.split(":");
                    evtName = String(arr[0]);
                    evtValue = arr[1];
                }
                else
                {
                    evtName = value.operValue;
                }
                if (evtName)
                {
                    Dispatcher.dispatchEvent(new DataEvent(evtName, evtValue));
                }
            }
            else if (value.name == "" + Language.getString(71044))
            {
                info = Cache.instance.task.getTaskInfoByGroup(ETaskGroup._ETaskGroupGuild);
                if (info == null)
                {
                    MsgManager.showRollTipsMsg("" + Language.getString(71045));
                }
                else
                {
                    TaskUtil.linkTask(info, isFly);
                }
            }
            else if (value.operType == EDailyOperType._EDailyOperTypePathing)
            {
                if (value.operValue.indexOf("#") > -1 || value.operValue.indexOf("#") > -1 && isFly)
                {
                    arr2 = value.operValue.split("#");
                    mapId = int(arr2[0]);
                    mapX = int(arr2[1]);
                    mapY = int(arr2[2]);
                    if (isFly)
                    {
                        sceneInfo = SceneConfig.instance.getSceneInfo(mapId);
                        if (sceneInfo && SPassPoint(sceneInfo.passInfos[(sceneInfo.passInfos.length - 1)]).passTo.length > 0)
                        {
                            sPassTo = new SPassTo();
                            point = new SPoint();
                            sPassTo.mapId = mapId;
                            point.x = mapX;
                            point.y = mapY;
                            sPassTo.toPoint = point;
                            AIManager.onAIControl(AIType.FlyBoot, sPassTo);
                        }
                    }
                    else
                    {
                        AIManager.onAIControl(AIType.GoToOtherMap, MapFileUtil.mapID, mapId, new Point(mapX, mapY));
                    }
                }
                else if (isFly)
                {
                    var gotoNPC:* = function () : void
            {
                AIManager.gotoOrSendtoNpc(int(value.operValue), isFly);
                return;
            }// end function
            ;
                    if (!FlyBootAI.checkCanFly())
                    {
                        return;
                    }
                    GameController.guide.checkAndBuyFlyBoot(gotoNPC);
                }
                else
                {
                    cache = Cache.instance.daily;
                    if (isInclude(value.displayGroup, EDailyType._EDailyTypeTask))
                    {
                        if (int(cache.data.times[value.type]) < int(value.times))
                        {
                            AIManager.gotoOrSendtoNpc(int(value.operValue));
                        }
                        else
                        {
                            MsgManager.showRollTipsMsg(value.name + "" + Language.getString(71046));
                        }
                    }
                    else
                    {
                        AIManager.gotoOrSendtoNpc(int(value.operValue));
                    }
                }
            }
            return;
        }// end function

        private static function getGroupById(param1:int) : int
        {
            if (param1 == 3000)
            {
                return ETaskGroup._ETaskGroupGuild;
            }
            if (param1 == 3001)
            {
                return ETaskGroup._ETaskGroupLoop;
            }
            if (param1 == 3002)
            {
                return ETaskGroup._ETaskGroupAutoFight;
            }
            return ETaskGroup._ETaskGroupPvp;
        }// end function

        public static function get maxActivity() : int
        {
            var _loc_1:TActivityConfig = null;
            return GameConst.TTodayAddActicvityMaxValue;
            return 100;
        }// end function

        public static function getReward(param1:String, param2:String = "#", param3:String = null) : Array
        {
            var _loc_6:ItemData = null;
            var _loc_8:String = null;
            var _loc_9:Array = null;
            if (param1 == null || param1 == "")
            {
                return [];
            }
            var _loc_4:* = param1.split(param2);
            var _loc_5:Array = [];
            var _loc_7:* = Cache.instance.role.entityInfo.career;
            for each (_loc_8 in _loc_4)
            {
                
                if (param3)
                {
                    _loc_9 = _loc_8.split(param3);
                    _loc_6 = new ItemData(int(_loc_9[0]));
                    _loc_6.itemAmount = int(_loc_9[1]);
                }
                else
                {
                    _loc_6 = new ItemData(int(_loc_8));
                }
                if (int(_loc_8) > 1000)
                {
                    if (_loc_6.itemInfo.career > 0 && _loc_6.itemInfo.career != _loc_7)
                    {
                        continue;
                    }
                }
                _loc_5.push(_loc_6);
            }
            return _loc_5;
        }// end function

        public static function getGiftReward(param1:int, param2:String = "#", param3:String = null) : Array
        {
            var _loc_8:ItemData = null;
            var _loc_10:String = null;
            var _loc_11:Array = null;
            if (param1 == 0)
            {
                return [];
            }
            var _loc_4:* = int(param1);
            var _loc_5:* = ItemConfig.instance.getConfig(_loc_4) as ItemGiftBagInfo;
            var _loc_6:* = _loc_5.itemList1.split(param2);
            var _loc_7:Array = [];
            var _loc_9:* = Cache.instance.role.entityInfo.career;
            for each (_loc_10 in _loc_6)
            {
                
                if (param3)
                {
                    _loc_11 = _loc_10.split(param3);
                    _loc_8 = new ItemData(int(_loc_11[1]));
                    _loc_8.itemAmount = int(_loc_11[2]);
                }
                else
                {
                    _loc_8 = new ItemData(int(_loc_10));
                }
                if (int(_loc_10) > 1000)
                {
                    if (_loc_8.itemInfo.career > 0 && _loc_8.itemInfo.career != _loc_9)
                    {
                        continue;
                    }
                }
                _loc_7.push(_loc_8);
            }
            return _loc_7;
        }// end function

        public static function isComplete(param1:TDailyViewConfig) : Boolean
        {
            return int(param1.activityValue) > 0 && int(Cache.instance.daily.data.times[param1.type]) >= int(param1.activityValue);
        }// end function

        public static function get openSoonItem() : TDailyViewConfig
        {
            var _loc_2:TDailyViewConfig = null;
            var _loc_5:Date = null;
            var _loc_6:TDailyViewConfig = null;
            var _loc_7:String = null;
            var _loc_8:int = 0;
            var _loc_1:* = int.MAX_VALUE;
            var _loc_3:* = DailyUtil.getDataByGroup(EDailyType._EDailyTypeActivity);
            var _loc_4:* = ClockManager.instance.nowDate;
            for each (_loc_6 in _loc_3)
            {
                
                if (_loc_6.normalOpenDt && _loc_6.openWeekDay == null)
                {
                    _loc_7 = _loc_6.normalOpenDt.replace("\"\"", "");
                    if (_loc_7.indexOf("#") != -1)
                    {
                        _loc_5 = DateUtil.getDateByHourAndMin(_loc_7.split("#")[0]);
                    }
                    else
                    {
                        _loc_5 = DateUtil.getDateByHourAndMin(_loc_7);
                    }
                    _loc_8 = DateUtil.getSecondsDis(_loc_4, _loc_5);
                    if (_loc_8 >= 0 && _loc_8 < _loc_1)
                    {
                        _loc_1 = _loc_8;
                        _loc_2 = _loc_6;
                    }
                }
            }
            return _loc_2;
        }// end function

        public static function getIsInDay(param1:int) : Boolean
        {
            var _loc_2:* = DailyConfig.instance.getDailyItem(param1);
            var _loc_3:* = ClockManager.instance.nowDate;
            var _loc_4:* = _loc_3.day == 0 ? ("7") : (_loc_3.day.toString());
            return _loc_2.openWeekDay.indexOf(_loc_4) != -1;
        }// end function

        public static function getTimeStr(param1:String, param2:Boolean = true) : String
        {
            if (param2)
            {
                return param1.replace("#", "~").replace("&", ",").replace("\"\"", "");
            }
            return param1.replace("#", "~").replace("&", ",");
        }// end function

        public static function hasActiveReward() : Boolean
        {
            var _loc_4:TActivityConfig = null;
            var _loc_1:* = DailyConfig.instance.allActivityData;
            var _loc_2:Boolean = false;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_1.length)
            {
                
                _loc_4 = _loc_1[_loc_3];
                if (_loc_2)
                {
                    break;
                }
                if (_loc_4.activity <= GameConst.TTodayAddActicvityMaxValue)
                {
                    _loc_2 = false;
                    if (Cache.instance.daily.data.level >= _loc_4.id)
                    {
                        _loc_2 = false;
                    }
                    else if (Cache.instance.daily.data.activity >= _loc_4.activity)
                    {
                        _loc_2 = true;
                    }
                    else
                    {
                        _loc_2 = false;
                    }
                }
                else
                {
                    _loc_2 = false;
                }
                _loc_3++;
            }
            return _loc_2;
        }// end function

        public static function isInActiveTime(param1:TDailyViewConfig) : Boolean
        {
            var _loc_3:Array = null;
            var _loc_4:Date = null;
            var _loc_5:Date = null;
            if (param1 != null && param1.openWeekDay != null)
            {
                if (getIsInDay(param1.id) == false)
                {
                    return false;
                }
            }
            var _loc_2:Boolean = true;
            if (param1.normalOpenDt == null)
            {
                return true;
            }
            if (param1.normalOpenDt.indexOf("#") != -1)
            {
                _loc_3 = param1.normalOpenDt.split("#");
                _loc_4 = DateUtil.getDateByHourAndMin(String(_loc_3[0]));
                _loc_5 = DateUtil.getDateByHourAndMin(String(_loc_3[1]));
                _loc_2 = ClockManager.instance.nowDate.time < _loc_5.time;
            }
            return _loc_2;
        }// end function

        public static function isOpenNow(param1:TDailyViewConfig) : Boolean
        {
            var _loc_3:Array = null;
            var _loc_4:Date = null;
            var _loc_5:Date = null;
            var _loc_2:Boolean = true;
            if (param1.normalOpenDt == null)
            {
                return true;
            }
            if (param1.normalOpenDt.indexOf("#") != -1)
            {
                _loc_3 = param1.normalOpenDt.split("#");
                _loc_4 = DateUtil.getDateByHourAndMin(String(_loc_3[0]));
                _loc_5 = DateUtil.getDateByHourAndMin(String(_loc_3[1]));
                _loc_2 = DateUtil.isInDate(_loc_4, _loc_5);
            }
            return _loc_2;
        }// end function

        public static function getCanSelectIndex(param1:Array) : int
        {
            var _loc_3:TDailyViewConfig = null;
            if (param1 == null || param1.length == 0)
            {
                return 0;
            }
            var _loc_2:int = 0;
            while (_loc_2 < param1.length)
            {
                
                _loc_3 = param1[_loc_2];
                if (isInActiveTime(_loc_3))
                {
                    return _loc_2;
                }
                _loc_2++;
            }
            return 0;
        }// end function

        public static function sortActive(param1:TDailyViewConfig, param2:TDailyViewConfig) : int
        {
            if (isInActiveTime(param1) && !isInActiveTime(param2))
            {
                return -1;
            }
            if (!isInActiveTime(param1) && isInActiveTime(param2))
            {
                return 1;
            }
            var _loc_3:* = Cache.instance.daily.todayNotOpenActives;
            var _loc_4:* = _loc_3.indexOf(param1.id) == -1;
            var _loc_5:* = _loc_3.indexOf(param2.id) == -1;
            if (_loc_4 && !_loc_5)
            {
                return -1;
            }
            if (!_loc_4 && _loc_5)
            {
                return 1;
            }
            if (param1.id < param2.id)
            {
                return -1;
            }
            if (param1.id > param2.id)
            {
                return 1;
            }
            return 0;
        }// end function

        public static function getBeginIndex() : int
        {
            if (curArr == null)
            {
                return 0;
            }
            var _loc_1:* = curArr[0];
            if (isOpenNow(_loc_1))
            {
                return 1;
            }
            return 0;
        }// end function

    }
}
